from public import save_log
from .bom import bom_check_net, bom_check_main
from .c import against
from .node import noise
from typing import AsyncIterable
import gradio as gr


async def itErr(aiselect:list, circuit, net, initial, bom_file, request: gr.Request)->AsyncIterable[str]:
    save_log( 'AI分析', request)
    result = ''
    if circuit and str(circuit)!=net: result+='#### ⚠️电路脚本被修改, 分析只按原生脚本进行\n\n'
    if '全部' in aiselect or '极性电容反接校验' in aiselect:
        cerr = against(circuit, net, initial)
        if cerr: 
            result+= '\n'.join([f'> ### 极性电容反接校验:', '```',*[f'🚫{e}' for e in cerr], '```'])
        else:
            result+= '```\n✅没有极性电容错误情况\n```'
        result+= '\n\n'
        yield result
    if '全部' in aiselect or '节点噪声分析' in aiselect:
        result+= '> ### 节点噪声分析:\n'
        result+= '💡噪音分析内容为可能存在的情况, 请以实际fft仿真结果为准\n\n'
        yield result
        async for text in noise(circuit):
            result+=text
            yield result
        result+= '\n\n'
        yield result
    bom_chs = []
    if '全部' in aiselect or 'BOM文件校验-网表' in aiselect:
        bom_chs.append(bom_check_net(circuit, bom_file and bom_file.name))
    if '全部' in aiselect or 'BOM文件校验-总表' in aiselect:
        bom_chs.append(bom_check_main(bom_file and bom_file.name))
    if bom_chs:
        result+='> ### BOM文件校验:\n💡新料和未填写物料编号的数据将不参与总表校验\n\n```\n'
        result+= '\n\n'.join(bom_chs)
        result+='\n```'
        yield result
    yield result